package com.gpipe.dbwriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.GZIPInputStream;

import com.gpipe.dirwatcher.DirectorySnapshot;
import com.gpipe.dirwatcherRealtime.*;
import com.gpipe.properties.InputProperties;
import com.gpipe.tools.*;

public class LoadDataByDirectory {
    //用来替代beginStartTime，给“职住模块”判断一天数据已跑完。
    String dayEndFlag = null;

    /*
     * author:zhengrx(move shenghong's codes)
     */
    public void MysqlDayList() throws SQLException {
        Map<String, String> _files = DirectorySnapshot.files;
        Collection<String> c = _files.values();
        Iterator it = c.iterator();

        // create mysql connection
        Mysql mysql = new Mysql();
        Connection connection = mysql.createConnect();
        while (it.hasNext()) {
            // we need to read files and write to mysql
            String _fileName = it.next().toString();
            GpipeInit.writeLogs(_fileName + " is writing to mysql server, please wait.......");
            mysql.writeMysql(_fileName, connection);
        }

        it.remove();
        c.clear();
        _files.clear();
        if (!connection.isClosed()) {
            connection.close();
        }

    }

    private void CommonPart() {
        // nbr|lac|ci:starttime|endtime
        Map<String, String> midBaseNbr1 = new HashMap<String, String>();
        // DirectorySnapshotRealtime.baseNbrArea = new HashMap<String,
        // String>();

        int startTimeIndex = 11;
        int lacIndex = 9;
        int ciIndex = 10;
        int nbrIndex = 5;

        Map<String, String> _files = DirectorySnapshotRealtime.oneHourFiles;
        Collection<String> c = _files.values();
        Iterator it = c.iterator();

        while (it.hasNext()) {
            String _fileName = it.next().toString();
            BufferedReader reader = null;
            try {
                reader = GpipeInit.unZipOutReader(_fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            // try {
            // reader = new BufferedReader(new InputStreamReader(
            // new FileInputStream(_fileName)));
            // } catch (FileNotFoundException e) {
            // e.printStackTrace();
            // }
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    String[] fields = null;
                    fields = line.split("\\|", -1);
                    if (fields.length >= 12) {
                        if (GpipeInit.isNotEmpty(fields[nbrIndex])) {
                            String key1 = fields[nbrIndex] + "|" + fields[lacIndex] + "|" + fields[ciIndex];
                            if (midBaseNbr1.containsKey(key1)) {
                                String value1 = midBaseNbr1.get(key1);
                                value1 = start2endTime(value1, fields[startTimeIndex]);
                                midBaseNbr1.put(key1, value1);
                            } else {
                                // if(fields[startTimeIndex]==null){
                                // System.out.println(key1+":"+fields[startTimeIndex]
                                // + "|" + fields[startTimeIndex]);
                                // }
                                // fieldstmp=fields;
                                // for(int i=0;i<fields.length;i++){
                                // System.out.println("--##--zhengxp:fields["+i+"]="+fields[i]);
                                // }

                                midBaseNbr1.put(key1, fields[startTimeIndex] + "|" + fields[startTimeIndex]);

                            }
                        }
                    }
                }

                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
//        it.remove();
        c.clear();
        _files.clear();

        Iterator iter1 = midBaseNbr1.entrySet().iterator();
        while (iter1.hasNext()) {
            Map.Entry entry = (Map.Entry) iter1.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            int duration = 0;
            try {
                duration = (int) (Double.parseDouble(val.split("\\|", -1)[1]) - Double.parseDouble(val.split("\\|", -1)[0]));
            } catch (NumberFormatException e) {
                System.out.println("error val:" + val);
                continue;
            }

            String newKey = key.split("\\|", -1)[0];
            String newVal = key.split("\\|", -1)[1] + "|" + key.split("\\|", -1)[2] + "|" + duration;

            if (DirectorySnapshotRealtime.baseNbrArea.containsKey(newKey)) {
                int durationOld = Integer.parseInt(DirectorySnapshotRealtime.baseNbrArea.get(newKey).split("\\|", -1)[2]);
                if (duration - durationOld > 0) {
                    DirectorySnapshotRealtime.baseNbrArea.put(newKey, newVal);
                }
            } else {
                DirectorySnapshotRealtime.baseNbrArea.put(newKey, newVal);
            }

        }
        iter1.remove();
        midBaseNbr1.clear();

        String filePath = GpipeInit.getPath();
        DirectorySnapshotRealtime.homePath = filePath.substring(0, filePath.lastIndexOf(File.separator));

    }

    private String start2endTime(String value1, String startTime) {
        String result = value1;
        String str[] = value1.split("\\|");
        try {
            if (str.length == 2) {
                if (Double.parseDouble(startTime) < Double.parseDouble(str[0])) {
                    result = startTime + "|" + str[1];
                } else if (Double.parseDouble(startTime) > Double.parseDouble(str[1])) {
                    result = str[0] + "|" + startTime;
                }
            }

        } catch (NumberFormatException e) {
            System.out.println("error value1:" + value1 + "\n error startTime:" + startTime);
            return result;
        }

        return result;
    }

    private void groupingOptimize(String lac, String ci, String nbr, String startTime, String endTime) {
        //nbr:Object(lac|ci|nbr|starttime|endtime|duration) -- second periodd
//        DirectorySnapshotRealtime.listSumRecords
        String timePeriod = null;
        if (Conf.mode == -1) {
            timePeriod = DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime;
        } else {
            String beginTimePeriod = GpipeInit.get15minBefore(endTime);
            String endTimePeriod = GpipeInit.getIncreTimePeriod(beginTimePeriod, InputProperties.inputdataConf.get("timePeriod"), "HHmm");
            timePeriod = beginTimePeriod + "-" + endTimePeriod;
        }
        SignalList signalList = new SignalList(lac, ci, nbr, startTime, endTime, timePeriod);
        List<SignalList> staList = DirectorySnapshotRealtime.listSumIncreRecords.get(nbr);
        if (staList == null) {
            staList = new ArrayList<SignalList>();
        }
        staList.add(signalList);
        DirectorySnapshotRealtime.listSumIncreRecords.put(nbr, staList);
    }

    //second period :group by nbr order by starttime sum(the same lac|ci 's duration) in a period
    //this is group by nbr
    private void grouping(String lac, String ci, String nbr, String startTime, String endTime) {
        //nbr:Object(lac|ci|nbr|starttime|endtime|duration) -- second periodd
//        DirectorySnapshotRealtime.listSumRecords
        String timePeriod = DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime;
        SignalList signalList = new SignalList(lac, ci, nbr, startTime, endTime, timePeriod);
        List<SignalList> staList = DirectorySnapshotRealtime.listSumRecords.get(nbr);
        if (staList == null) {
            staList = new ArrayList<SignalList>();
        }
        staList.add(signalList);
        DirectorySnapshotRealtime.listSumRecords.put(nbr, staList);
    }

    int lacIndex = 0;
    int ciIndex = 0;
    int nbrIndex = 0;
    int startTimeIndex = 0;
    int endTimeIndex = 0;
    int maxIndex = 0;

    private void init() {
        lacIndex = Integer.valueOf(InputProperties.inputdataConf.get("lac|ci").trim().split("\\|", -1)[0]);
        ciIndex = Integer.valueOf(InputProperties.inputdataConf.get("lac|ci").trim().split("\\|", -1)[1]);
        nbrIndex = Integer.valueOf(InputProperties.inputdataConf.get("nbr").trim());
        startTimeIndex = Integer.valueOf(InputProperties.inputdataConf.get("startTime").trim());
        endTimeIndex = Integer.valueOf(InputProperties.inputdataConf.get("endTime").trim());
        int[] listIndex = new int[5];
        listIndex[0] = lacIndex;
        listIndex[1] = ciIndex;
        listIndex[2] = nbrIndex;
        listIndex[3] = startTimeIndex;
        listIndex[4] = endTimeIndex;
        Arrays.sort(listIndex);
        maxIndex = listIndex[listIndex.length - 1];

    }


    /**
     * 说明：二期数据处理入口
     */
    public void Mysql2period() {
        init();

        //**清单汇总层数据处理
//        listSumLevel();--弃用
        listSumLevelOptimize();

        //**应用层数据处理
        //职住交通区数据处理--单独启用程序2来跑
//        workLifeDual();--弃用
        workLifeDualOptimize();

        //清单汇总层-OD量大中小区数据处理
        ODProcess();
        ODSumProcess();


        //集散量大中小区数据处理
        amountProcess();


        //接口层数据清理
        interfaceDual();

    }
    public void test(String date){
        Tools tools = new Tools();
//        String filePathConn = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.subwayBusConn;
        String filePathConn = "E:\\testLog\\data"+File.separator + Conf.subwayBusConn;
//        String filePathConn = "E:\\testLog\\data"+File.separator + "tt.txt";
        //读取公交和地铁接驳的本地数据
        BufferedReader readerLocConn = tools.readLocFile(filePathConn);
        String lineLocConn = null;
        List<String[]> busSubwayStr = new ArrayList<>();
        if (readerLocConn != null) {
            try {
                while ((lineLocConn = readerLocConn.readLine()) != null) {
                    BusSubwayConn busSubwayConn = new BusSubwayConn(lineLocConn);
                    busSubwayStr.add((busSubwayConn.getKey()+"|"+busSubwayConn.getOd_sum()).split("\\|",-1));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(String str:busSubwayStr.get(0)){
            System.out.println("--zxp--:"+str);
        }

        String dSql = "delete from od_summ_2period_connection_day where date ='"+date+"'";
        String wSql = "INSERT INTO od_summ_2period_connection_day (date, type, station_id_o, station_name_o, station_id_d, station_name_d, speed, od_sum) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
        Mysql mysql = new Mysql();
        Connection conn = mysql.tmpConnect();

        mysql.updateMysql(dSql, conn);
        mysql.writeMysql(busSubwayStr, wSql, conn);
        busSubwayStr.clear();
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void interfaceDual() {
        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);
        String timeFlag = DirectorySnapshotRealtime.periodEndTime;

        if ((Long.parseLong(time) > 2345 && Long.parseLong(time) <= 2359) || timeFlag.equals("0000")) {
            Tools tools = new Tools();
            String filePathAmount = tools.getPath() + File.separator + Conf.midPath + File.separator + "amount.txt";
            String filePathODList = tools.getPath() + File.separator + Conf.midPath + File.separator + "ODList.txt";
            String filePathListSum = tools.getPath() + File.separator + Conf.midPath + File.separator + "listSum.txt";
            String filePathConn = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.subwayBusConn;

            String path1 = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath;
            String path2 = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.ODLPath;

            //读取公交和地铁接驳的本地数据
            BufferedReader readerLocConn = tools.readLocFile(filePathConn);
            String lineLocConn = null;
            List<String[]> busSubwayStr = new ArrayList<>();
            if (readerLocConn != null) {
                try {
                    while ((lineLocConn = readerLocConn.readLine()) != null) {
                        BusSubwayConn busSubwayConn = new BusSubwayConn(lineLocConn);
                        busSubwayStr.add((busSubwayConn.getKey()+"|"+busSubwayConn.getOd_sum()).split("\\|",-1));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String dSql = "delete from od_summ_2period_connection_day where date ='"+date+"'";
            String wSql = "INSERT INTO od_summ_2period_connection_day (date, type, station_id_o, station_name_o, station_id_d, station_name_d, speed, od_sum) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
            Mysql mysql = new Mysql();
            Connection conn = mysql.createConnect();

            mysql.updateMysql(dSql, conn);
            mysql.writeMysql(busSubwayStr, wSql, conn);
            busSubwayStr.clear();

            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //一天结束后，清空该文件
            tools.writeLocFile(filePathAmount, "");
            tools.writeLocFile(filePathODList, "");
            tools.writeLocFile(filePathListSum, "");
            tools.writeLocFile(filePathConn, "");
            tools.deleteFile(path1, date);
            tools.deleteFile(path2, date);

            Process ps;
            String interfaceShell = tools.getPath() + File.separator + Conf.shellPath + File.separator + Conf.interfaceShell + " " + date;
            GpipeInit.writeLogs("--info--接口数据处理脚本:" + interfaceShell);
            try {
                ps = Runtime.getRuntime().exec(interfaceShell);
                ps.waitFor();
                GpipeInit.writeLogs("--info--接口数据处理完成，" + interfaceShell);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.exit(0);
        }

    }

    public void amountProcess() {
        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);

        String ST = GpipeInit.writeLogs("--------Amount process!-second period--------");


        Mysql mysql = new Mysql();
        Connection conn = mysql.createConnect();
        String timePeriod = new String();
        DirectorySnapshotRealtime.amountSumRecords.clear();
        if (Conf.mode == -1) {


            timePeriod = DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime;


        } else {
            //实时逻辑
//            Iterator iterAmountSum = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
////            List<String[]> amountStr = new ArrayList<>();
//            while (iterAmountSum.hasNext()) {
//                Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterAmountSum.next();
//                String key = entry.getKey();
//                List<SignalList> val = entry.getValue();
//                for (SignalList sl : val) {
//                    if (sl.getDuration() >= Conf.amountPeriod) {
//                        if (DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi()) != null) {
//                            AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi());
//                            as.getAmountSum().incrementAndGet();
//                        } else {
//                            AmountSum as = new AmountSum(sl.getLacCi());
//                            DirectorySnapshotRealtime.amountSumRecords.put(sl.getLacCi(), as);
//                        }
//
//                    }
//                }
//            }

            String beginTimePeriod = GpipeInit.get15minBefore(time);
            String endTimePeriod = GpipeInit.getIncreTimePeriod(beginTimePeriod, InputProperties.inputdataConf.get("timePeriod"), "HHmm");
            timePeriod = beginTimePeriod + "-" + endTimePeriod;


        }

        Tools tools = new Tools();

        /*新版本逻辑，集散量是两个指标，分别是集合点和离散点，基于OD的清单数据
        判断停留时长，如果大于设定值Conf.amountPeriod,则认为该记录为该点的集散点*/
        //先从本地读出上个周期集散量的处理结果
        //lacCiO|lacCiD|amountsum
        String filePathAmount = tools.getPath() + File.separator + Conf.midPath + File.separator + "amount.txt";
        BufferedReader readerLocAmount = tools.readLocFile(filePathAmount);
        String lineLocAmount = null;
        try {
            if (readerLocAmount != null) {
                while ((lineLocAmount = readerLocAmount.readLine()) != null) {
                    String[] cols = lineLocAmount.split("\\|", -1);
                    //lacCiO|lacCiD
                    String lacCiOD = cols[0] + "|" + cols[1] + "|" + cols[2] + "|" + cols[3];
                    String lacCiO = cols[0] + "|" + cols[1];
                    String lacCiD = cols[2] + "|" + cols[3];
                    if (DirectorySnapshotRealtime.amountSumRecords.get(lacCiOD) != null) {
                        AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(lacCiOD);
                        as.getAmountSum().incrementAndGet();
                    } else {
                        AmountSum as = new AmountSum(lacCiO, lacCiD, cols[4]);
                        DirectorySnapshotRealtime.amountSumRecords.put(lacCiOD, as);
                    }
                }
                readerLocAmount.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.ODLPath + File.separator + date + "-" + timePeriod + ".txt";
        BufferedReader readerLoc = tools.readLocFile(filePath);
        String lineLoc = null;
        AtomicInteger i = new AtomicInteger(0);
        AtomicInteger j = new AtomicInteger(0);
        //数据格式date,timePeriod,nbr,beginTime,arriveTime,duration,lacCiO,lacCiD,distance

        try {
            if (readerLoc != null) {
                while ((lineLoc = readerLoc.readLine()) != null) {
                    i.incrementAndGet();
                    String[] cols = lineLoc.split("\\|", -1);
                    //留意lacci是用|作为分隔符的
                    ODList odList = new ODList(cols[0], cols[1], cols[2], cols[3], cols[4], cols[6] + "|" + cols[7], cols[8] + "|" + cols[9]);
                    if (odList.getDuration() >= Conf.amountPeriod) {
                        j.incrementAndGet();
                        //DirectorySnapshotRealtime.amountSumRecords：记录集散量的记录 -- second period--lacciO|lacciD:amountsum
                        if (DirectorySnapshotRealtime.amountSumRecords.get(odList.getLacCiOD()) != null) {
                            AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(odList.getLacCiOD());
                            as.getAmountSum().incrementAndGet();
                        } else {
                            AmountSum as = new AmountSum(odList.getLacCiO(), odList.getLacCiD());
                            DirectorySnapshotRealtime.amountSumRecords.put(odList.getLacCiOD(), as);
                        }
                    }
                }
                readerLoc.close();
            }
            GpipeInit.writeLogs("--info--集散量处理中总记录数：" + i + ",时长超过2小时的记录数：" + j + ",占比：" + Math.floor(j.intValue() / i.intValue() * 100) + "%");

        } catch (IOException e) {
            e.printStackTrace();
        }

        //把本周期集散量的处理结果存储在本地
        //lacciO|lacciD|amountsum
        StringBuilder amountSum = new StringBuilder();
        Iterator iter = DirectorySnapshotRealtime.amountSumRecords.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, AmountSum> entry = (Map.Entry) iter.next();
            AmountSum val = entry.getValue();

            amountSum.append(val.getLacCiO() + "|" + val.getLacCiD() + "|" + val.getAmountSum() + "\n");
        }

//        System.out.println(amountSum.toString());
        tools.writeLocFile(filePathAmount, amountSum.toString());
        amountSum.delete(0, amountSum.length());


        /*旧版本逻辑，集散量是一个指标，基于单点的清单数据判断停留时长，
        如果大于设定值Conf.amountPeriod,则认为该记录为该点的集散点*/
        //先从本地读出上个周期集散量的处理结果
        //lac|ci|amountsum
//        String filePathAmount = tools.getPath() + File.separator + Conf.midPath + File.separator + "amount.txt";
//        BufferedReader readerLocAmount = tools.readLocFile(filePathAmount);
//        String lineLocAmount = null;
//        try {
//            if (readerLocAmount != null) {
//                while ((lineLocAmount = readerLocAmount.readLine()) != null) {
//                    String[] cols = lineLocAmount.split("\\|", -1);
//                    String lacCi = cols[0] + "|" + cols[1];
//                    if (DirectorySnapshotRealtime.amountSumRecords.get(lacCi) != null) {
//                        AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(lacCi);
//                        as.getAmountSum().incrementAndGet();
//                    } else {
//                        AmountSum as = new AmountSum(lacCi, cols[2]);
//                        DirectorySnapshotRealtime.amountSumRecords.put(lacCi, as);
//                    }
//                }
//                readerLocAmount.close();
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


//        String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath + File.separator + date + "-" + timePeriod + ".txt";
//        BufferedReader readerLoc = tools.readLocFile(filePath);
//        String lineLoc = null;
//        AtomicInteger i = new AtomicInteger(0);
//        AtomicInteger j = new AtomicInteger(0);
//        try {
//            if (readerLoc != null) {
//                while ((lineLoc = readerLoc.readLine()) != null) {
//                    i.incrementAndGet();
//                    String[] cols = lineLoc.split("\\|", -1);
//                    SignalList sl = new SignalList(cols[5], cols[6], cols[3], cols[7], cols[8], cols[2]);
//                    if (sl.getDuration() >= Conf.amountPeriod) {
//                        j.incrementAndGet();
//                        if (DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi()) != null) {
//                            AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi());
//                            as.getAmountSum().incrementAndGet();
//                        } else {
//                            AmountSum as = new AmountSum(sl.getLacCi());
//                            DirectorySnapshotRealtime.amountSumRecords.put(sl.getLacCi(), as);
//                        }
//                    }
//                }
//                readerLoc.close();
//            }
//            GpipeInit.writeLogs("--info--集散量处理中总记录数：" + i + ",时长超过2小时的记录数：" + j + ",占比：" + Math.floor(j.intValue() / i.intValue() * 100) + "%");
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        //把本周期集散量的处理结果存储在本地
//        //lac|ci|amountsum
//        StringBuilder amountSum = new StringBuilder();
//        Iterator iter = DirectorySnapshotRealtime.amountSumRecords.entrySet().iterator();
//        while (iter.hasNext()) {
//            Map.Entry<String, AmountSum> entry = (Map.Entry) iter.next();
//            AmountSum val = entry.getValue();
//
//            amountSum.append(val.getLacCi() + "|" + val.getAmountSum() + "\n");
//        }
//
////        System.out.println(amountSum.toString());
//        tools.writeLocFile(filePathAmount, amountSum.toString());
//        amountSum.delete(0, amountSum.length());


//            ResultSet resultSet = mysql.readMysql("select * from jw2_list_nbr_group_bts_sum where date='" + date + "' and timePeriod ='" + timePeriod + "'", conn);
//            try {
//
//                while (resultSet.next()) {
//                    SignalList sl = new SignalList(resultSet.getString(ConfOfMysqlModel.listSumModelParas[2]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[3]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[4]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[5]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[6]));
////                    List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]));
////                    if (val == null) {
////                        val = new ArrayList<SignalList>();
////                    }
//                    if (sl.getDuration() >= Conf.amountPeriod) {
////                        val.add(sl);
////                        DirectorySnapshotRealtime.listSumIncreRecords.put(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), val);
//
//                        if (DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi()) != null) {
//                            AmountSum as = DirectorySnapshotRealtime.amountSumRecords.get(sl.getLacCi());
//                            as.getAmountSum().incrementAndGet();
//                        } else {
//                            AmountSum as = new AmountSum(sl.getLacCi());
//                            DirectorySnapshotRealtime.amountSumRecords.put(sl.getLacCi(), as);
//
//
//                        }
//
//                    }
//
//                }
//                resultSet.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }


        GpipeInit.writeLogs("--info--:amountSumRecords.size=" + DirectorySnapshotRealtime.amountSumRecords.size());
        String timeFlag = DirectorySnapshotRealtime.periodEndTime;

        if ((Long.parseLong(time) > 2345 && Long.parseLong(time) <= 2359) || timeFlag.equals("0000")) {
            String amountDayST = GpipeInit.writeLogs("--------Amount data into mysql begin!-second period--------DirectorySnapshotRealtime.amountSumRecords.size=" + DirectorySnapshotRealtime.amountSumRecords.size());
            //根据集结量和离散量来统计结果
            //记录集结量和离散量的记录 -- second period--lacci:amounInOuttsum
            Map<String, AmountInOutSum> amountInOutRecords = new HashMap<String, AmountInOutSum>();

            Iterator iterAmountSum = DirectorySnapshotRealtime.amountSumRecords.entrySet().iterator();
            while (iterAmountSum.hasNext()) {
                Map.Entry<String, AmountSum> entry = (Map.Entry) iterAmountSum.next();
                String key = entry.getKey();
                AmountSum val = entry.getValue();

                if (amountInOutRecords.get(val.getLacCiO()) != null) {
                    AmountInOutSum as = amountInOutRecords.get(val.getLacCiO());
                    as.getAmountOutSum().addAndGet(Integer.parseInt(val.getAmountSum().toString()));
                } else {
                    AmountInOutSum as = new AmountInOutSum(val.getLacCiO(), "0", "0");
                    amountInOutRecords.put(val.getLacCiO(), as);
                }


                if (amountInOutRecords.get(val.getLacCiD()) != null) {
                    AmountInOutSum as = amountInOutRecords.get(val.getLacCiD());
                    as.getAmountInSum().addAndGet(Integer.parseInt(val.getAmountSum().toString()));
                } else {
                    AmountInOutSum as = new AmountInOutSum(val.getLacCiD(), "0", "0");
                    amountInOutRecords.put(val.getLacCiD(), as);
                }


            }

            DirectorySnapshotRealtime.amountSumRecords.clear();

            Iterator iterAmountInOutSum = amountInOutRecords.entrySet().iterator();
            List<String[]> amountStr = new ArrayList<>();
            while (iterAmountInOutSum.hasNext()) {
                Map.Entry<String, AmountInOutSum> entry = (Map.Entry) iterAmountInOutSum.next();
                String key = entry.getKey();
                AmountInOutSum val = entry.getValue();


                String[] s = new String[]{date, val.getLacCi(), val.getB_area_id(), val.getB_area_name(), val.getM_area_id(), val.getM_area_name(), val.getS_area_id(), val.getS_area_name(), String.valueOf(val.getAmountInSum().intValue()), String.valueOf(val.getAmountOutSum().intValue())};
                amountStr.add(s);
            }

            //旧版本的逻辑，即将被弃用
//            String amountDayST = GpipeInit.writeLogs("--------Amount data into mysql begin!-second period--------DirectorySnapshotRealtime.amountSumRecords.size=" + DirectorySnapshotRealtime.amountSumRecords.size());
//            Iterator iterAmountSum = DirectorySnapshotRealtime.amountSumRecords.entrySet().iterator();
//            List<String[]> amountStr = new ArrayList<>();
//            while (iterAmountSum.hasNext()) {
//                Map.Entry<String, AmountSum> entry = (Map.Entry) iterAmountSum.next();
//                String key = entry.getKey();
//                AmountSum val = entry.getValue();
//                String[] s = new String[]{date, val.getLacCi(), val.getB_area_id(), val.getB_area_name(), val.getM_area_id(), val.getM_area_name(), val.getS_area_id(), val.getS_area_name(), String.valueOf(val.getAmountSum().intValue())};
//                amountStr.add(s);
//            }


            String date7Day = GpipeInit.getIncreDayPeriodOptional(date, Conf.delPeriod, "yyyyMMdd", "yyyyMMdd");
            String wSql = "INSERT INTO jw2_app_amount_day (date,lacCi,b_area_id,b_area_name,m_area_id,m_area_name,s_area_id,s_area_name,amount_in,amount_out)VALUES(?,?,?,?,?,?,?,?,?,?)";
            String dSqlBefore = "delete from jw2_app_amount_day where date =" + date7Day;
            String dSql = "delete from jw2_app_amount_day where date =" + date;

            mysql.updateMysql(dSqlBefore, conn);
            mysql.updateMysql(dSql, conn);
            mysql.writeMysql(amountStr, wSql, conn);
            amountInOutRecords.clear();
            amountStr.clear();

            String amountDayET = GpipeInit.writeLogs("--------Amount data into mysql success!-second period--------");
            GpipeInit.writeLogs("--info--:Amount data into mysql total time :" + GpipeInit.duration(amountDayST, amountDayET, "yyyy-MM-dd HH:mm:ss") + "s");
        } else {
            DirectorySnapshotRealtime.amountSumRecords.clear();
        }

        String ET = GpipeInit.writeLogs("--------Amount process success!-second period--------");
        GpipeInit.writeLogs("--info--:Amount process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss") + "s");

    }


    /**
     * 逻辑：1、如果是实时模式则通过DirectorySnapshot.lastFileName,判断出最近的半小时
     * 区间，从mysql读取数据，写入DirectorySnapshotRealtime.ODListcreRecords，2、如果是
     * 批量模式，根据DirectorySnapshotRealtime.periodEndTime判断出最近的半小时区间，从
     * mysql读取数据，写入DirectorySnapshotRealtime.ODListcreRecords
     */
    public void ODSumProcess() {
        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String date = fileTime.substring(0, 8);
        String date7Day = GpipeInit.getIncreDayPeriodOptional(date, Conf.delPeriod, "yyyyMMdd", "yyyyMMdd");

        int startFlog = -1;//1：开始跑半个小时周期的数据；-1：数据先不跑，等下一轮
        String sql = new String();
        String BT = new String();
        String timePeriod = new String();
        String[] periods = new String[2];
        if (Conf.mode == -1) {
            //批量模式
            String minute = DirectorySnapshotRealtime.periodEndTime.substring(2, 4);
            if (minute.equals("00") || minute.equals("30")) {
                String beginTime = GpipeInit.getIncreTimePeriodOptional(DirectorySnapshotRealtime.periodEndTime, "-30", "HHmm", "HHmm");
                String midTime = GpipeInit.getIncreTimePeriodOptional(DirectorySnapshotRealtime.periodEndTime, "-15", "HHmm", "HHmm");
                startFlog = 1;
                periods = new String[]{beginTime + "-" + midTime, midTime + "-" + DirectorySnapshotRealtime.periodEndTime};
//                sql = "select * from jw2_app_od_list where date='" + date + "' and timePeriod in ('" + beginTime + "-" + midTime + "','" + midTime + "-" + DirectorySnapshotRealtime.periodEndTime + "')";
                BT = GpipeInit.writeLogs("--info--:ODSum逻辑开始处理 data 从" + beginTime + "-" + DirectorySnapshotRealtime.periodEndTime + "时段的数据");
                timePeriod = beginTime + "-" + DirectorySnapshotRealtime.periodEndTime;
            } else {
                startFlog = -1;
            }
        } else {
            //实时模式
            //periodET<fileTime
            String periodET = GpipeInit.getPeriodBefore(fileTime, "30");
            String periodST = GpipeInit.getIncreTimePeriodOptional(periodET, "-30", "HHmm", "HHmm");
            String periodMT = GpipeInit.getIncreTimePeriodOptional(periodET, "-15", "HHmm", "HHmm");

//            DirectorySnapshotRealtime.periodFlag
            String flag = periodST + "-" + periodET;
            if (!flag.equals(DirectorySnapshotRealtime.periodFlag)) {
                DirectorySnapshotRealtime.periodFlag = flag;
                startFlog = 1;
                periods = new String[]{periodST + "-" + periodMT, periodMT + "-" + periodET};
//                sql = "select * from jw2_app_od_list where date='" + date + "' and timePeriod in ('" + periodST + "-" + periodMT + "','" + periodMT + "-" + periodET + "')";
                BT = GpipeInit.writeLogs("--info--:ODSum逻辑开始处理 data 从" + periodST + "-" + periodET + "时段的数据");
                timePeriod = periodST + "-" + periodET;
            } else {
                startFlog = -1;
            }
        }
        if (startFlog == 1) {
            Mysql mysql = new Mysql();
            Connection conn = mysql.createConnect();

//            //存放od汇总后的数据，lacCiO+"|"+lacCiD+"|"+commuting:odSum
//            HashMap<String, AtomicInteger> odSumMap = new HashMap<String, AtomicInteger>();
            //存放od汇总后的数据，lacCiO+"|"+lacCiD+"|"+commuting:ODListSum
            HashMap<String, ODListSum> odSumMap = new HashMap<String, ODListSum>();

            HashMap<String, WorkLife> workLifeMap = new HashMap<String, WorkLife>();
            HashMap<String, SubwayConn> connMap = new HashMap<>();
            HashMap<String, BusConn> busConnMap = new HashMap<>();
            //暂时先这么处理
            String workLifeSql = "select * from jw2_app_work_life_month;";
            ResultSet resultSetWL = mysql.readMysql(workLifeSql, conn);
            try {
                while (resultSetWL.next()) {
                    WorkLife workLife = new WorkLife(resultSetWL.getString(ConfOfMysqlModel.workLifeModelParas[2]), resultSetWL.getString(ConfOfMysqlModel.workLifeModelParas[3]));
                    workLifeMap.put(resultSetWL.getString(ConfOfMysqlModel.workLifeModelParas[1]), workLife);
                }
//                resultSetWL.close();
                GpipeInit.writeLogs("--test--:workLifeMap.size=" + workLifeMap.size());
            } catch (SQLException e) {
                e.printStackTrace();
            }

            DirectorySnapshotRealtime.ODListcreRecords.clear();

//            此处加入读取地铁接驳的配置信息
            String subwayConnSql = "select * from base_conf_module;";
            ResultSet resultSetSubwayConn = mysql.readMysql(subwayConnSql, conn);
            try {
                while (resultSetSubwayConn.next()) {

                    SubwayConn subwayConn = new SubwayConn(resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[0]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[1]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[2]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[3]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[4]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[5]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[6]), resultSetSubwayConn.getString(ConfOfMysqlModel.connModelParas[7]));
                    connMap.put(subwayConn.getKey(), subwayConn);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

//            此处加入读取公交接驳的配置信息
            String busConnSql = "select * from base_conf_bus_analyse;";
            ResultSet resultSetBusConn = mysql.readMysql(busConnSql, conn);
            try {
                while (resultSetBusConn.next()) {
                    BusConn busConn = new BusConn(resultSetBusConn);
                    busConnMap.put(busConn.getKey(), busConn);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            Tools tools = new Tools();

            //记录公交地铁接驳数据date|type|station_id_o|station_name_o|station_id_d|station_name_d|speed:ODList -- second period
            Map<String,AtomicInteger> tmpBusSubwayMap = new HashMap<>();

            //读取公交和地铁接驳的本地数据
            String filePathConn = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.subwayBusConn;
            BufferedReader readerLocConn = tools.readLocFile(filePathConn);
            String lineLocConn = null;
            if (readerLocConn != null) {
                try {
                    while ((lineLocConn = readerLocConn.readLine()) != null) {
                        BusSubwayConn busSubwayConn = new BusSubwayConn(lineLocConn);
                        tmpBusSubwayMap.put(busSubwayConn.getKey(),busSubwayConn.getOd_sum());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            //从本地读取ODList数据
            //数据格式date,timePeriod,nbr,beginTime,arriveTime,duration,lacCiO,lacCiD,distance

//            此处加入公交站和地铁接驳的逻辑
            for (String path : periods) {
                String filePathODList = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.ODLPath + File.separator + date + "-" + path + ".txt";
                BufferedReader readerLoc = tools.readLocFile(filePathODList);
                String lineLoc = null;
                try {
                    if (readerLoc != null) {
                        while ((lineLoc = readerLoc.readLine()) != null) {
                            String[] cols = lineLoc.split("\\|", -1);
                            //留意lacci是用|作为分隔符的
                            ODList odList = new ODList(cols[0], cols[1], cols[2], cols[3], cols[4], cols[6] + "|" + cols[7], cols[8] + "|" + cols[9]);
                            if (odList.getDuration() >= Conf.ODPeriod) {
                                //如果od的记录停留时长大于某个时间间隔（两个小时），该记录才被加入到od的统计当中
                                List<ODList> val = DirectorySnapshotRealtime.ODListcreRecords.get(cols[2]);
                                if (val == null) {
                                    val = new ArrayList<ODList>();
                                }
                                val.add(odList);
                                DirectorySnapshotRealtime.ODListcreRecords.put(cols[2], val);
                            }
                            //地铁接驳的处理逻辑 type=0
                            // tmpBusSubwayMap的key=date|type(0)|station_id_o|station_name_o|station_id_d(-1)|station_name_d(-1)|speed
                            if(connMap.containsKey(odList.getLacCiO())){
                                //符合地铁接驳配置信息，纳入统计
                                String keySubway = date+"|0|"+connMap.get(odList.getLacCiO()).getStationId()+"|"+connMap.get(odList.getLacCiO()).getStationName()+"|-1|-1|"+odList.getSpeed();
                                if(tmpBusSubwayMap.containsKey(keySubway)){
                                    tmpBusSubwayMap.get(keySubway).incrementAndGet();
                                }else {
                                    tmpBusSubwayMap.put(keySubway,new AtomicInteger(1));
                                }
                            }else if(busConnMap.containsKey(odList.getLacCiOD())){
                                //公交分析的处理逻辑 type=1
                                //tmpBusSubwayMap的key=date|type(1)|station_id_o|station_name_o|station_id_d|station_name_d|speed(-1)
                                //符合公交分析配置信息，纳入统计
                                String keyBus = date+"|1|"+busConnMap.get(odList.getLacCiOD()).getODidName()+"|-1";
                                if(tmpBusSubwayMap.containsKey(keyBus)){
                                    tmpBusSubwayMap.get(keyBus).incrementAndGet();
                                }else {
                                    tmpBusSubwayMap.put(keyBus,new AtomicInteger(1));
                                }
                            }
                        }
                        readerLoc.close();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //清理公交地铁接驳的配置
            busConnMap.clear();
            connMap.clear();
            //写回去本地文件
            StringBuilder listSumTmp =new StringBuilder();
            Iterator iterConn = tmpBusSubwayMap.entrySet().iterator();
            while (iterConn.hasNext()){
                Map.Entry<String,AtomicInteger> entry = (Map.Entry) iterConn.next();
                listSumTmp.append(entry.getKey()+"|"+entry.getValue()+"\n");
            }
            tools.writeLocFile(filePathConn, listSumTmp.toString());

            //test
//            test(date);

            listSumTmp.delete(0, listSumTmp.length());

            String odListcreRecordsET = GpipeInit.writeLogs("--------DirectorySnapshotRealtime.ODListcreRecords init success!-second period--------");
            GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.ODListcreRecords init total time :" + GpipeInit.duration(BT, odListcreRecordsET, "yyyy-MM-dd HH:mm:ss") + "s");

            GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.ODListcreRecords.size=" + DirectorySnapshotRealtime.ODListcreRecords.size());

            //开始循环ODListcreRecords根据laccio laccid为key来做号码的聚合
            Iterator iterODList = DirectorySnapshotRealtime.ODListcreRecords.entrySet().iterator();
            int i = 0;
            while (iterODList.hasNext()) {
                Map.Entry<String, List<ODList>> entry = (Map.Entry) iterODList.next();
                String key = entry.getKey();
                List<ODList> val = entry.getValue();

                for (ODList odList : val) {
                    i++;
                    WorkLife workLife = workLifeMap.get(odList.getNbr());
                    String workBts = null;
                    String lifeBts = null;
                    if (workLife != null) {
                        workBts = workLifeMap.get(odList.getNbr()).getWorkBts();
                        lifeBts = workLifeMap.get(odList.getNbr()).getLifeBts();
                    }

                    String oBts = odList.getLacCiO();
                    String dBts = odList.getLacCiD();
                    String commuting = new String();

                    if (oBts.equals(workBts) || oBts.equals(lifeBts) || dBts.equals(workBts) || dBts.equals(lifeBts)) {
                        commuting = "通勤";
                    } else {
                        commuting = "非通勤";
                    }

//                    odList.setCommuting(commuting);


                    String newKey = oBts + "|" + dBts + "|" + commuting;
                    if (odSumMap.get(newKey) != null) {
                        odSumMap.get(newKey).getOdSum().incrementAndGet();
                    } else {
                        ODListSum odListSum = new ODListSum();
                        odListSum.init(date, timePeriod, oBts, dBts, commuting);
                        odSumMap.put(newKey, odListSum);
                    }


//                    if (odSumMap.get(newKey) != null) {
//                        odSumMap.get(newKey).incrementAndGet();
//                    } else {
//                        odSumMap.put(newKey, new AtomicInteger(1));
//                    }
                }
            }

            System.out.println("--test--:DirectorySnapshotRealtime.ODListcreRecords=" + i);
            DirectorySnapshotRealtime.ODListcreRecords.clear();
            workLifeMap.clear();

            List<String[]> ODSum = new ArrayList<>();
            Iterator iterODSum = odSumMap.entrySet().iterator();
            while (iterODSum.hasNext()) {
                Map.Entry<String, ODListSum> entry = (Map.Entry) iterODSum.next();
//                String[] key = entry.getKey().split("\\|");
                ODListSum val = entry.getValue();
                String[] s = new String[]{val.getDate(), val.getTime(), val.getTimePeriod(), val.getS_area_id_o(), val.getS_area_name_o(), val.getM_area_id_o(), val.getM_area_name_o(), val.getB_area_id_o(), val.getB_area_name_o(), val.getS_area_id_d(), val.getS_area_name_d(), val.getM_area_id_d(), val.getM_area_name_d(), val.getB_area_id_d(), val.getB_area_name_d(), val.getCommuting(), String.valueOf(val.getOdSum()), String.valueOf(val.getDistance())};
                ODSum.add(s);
            }

            odSumMap.clear();

            String wSql = "INSERT INTO jw2_app_od_sum_" + date + "(date,time,timePeriod,s_area_id_o,s_area_name_o,m_area_id_o,m_area_name_o,b_area_id_o,b_area_name_o,s_area_id_d,s_area_name_d,m_area_id_d,m_area_name_d,b_area_id_d,b_area_name_d,commuting,s_area_od,distance) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            String cSql = "create table if not exists jw2_app_od_sum_" + date + " like jw2_app_od_sum_sample";
            String dSql = "drop table if exists jw2_app_od_sum_" + date7Day;


            mysql.updateMysql(dSql, conn);
            mysql.updateMysql(cSql, conn);
            mysql.writeMysql(ODSum, wSql, conn);

            ODSum.clear();

            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            String ET = GpipeInit.writeLogs("--------ODSumProcess process success!-second period--------");
            GpipeInit.writeLogs("--info--:ODSumProcess process total time :" + GpipeInit.duration(BT, ET, "yyyy-MM-dd HH:mm:ss") + "s");
        }
    }

    /**
     * 逻辑：根据jw2_list_nbr_group_bts_sum按照实施15min间隔/或者根据批量的15
     * 分钟间隔（也就是直接使用DirectorySnapshotRealtime.listSumIncreRecords）
     * ，按照时间顺序，依次选出od清单，计算od基站的距离，关联通勤、非通勤，然
     * 后写入清单表jw2_app_od_list（表每天一清理（第二天的1点清理-由于后面数据
     * 处理延迟30min），当天的记录按时段（15min）插入，包括大中小区）
     */
    public void ODProcess() {
        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);
        GpipeInit.writeLogs("--info--:OD data dual process,date:" + date);
        String ST = GpipeInit.writeLogs("--------OD process!-second period--------");
        GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.listSumIncreRecords.size=" + DirectorySnapshotRealtime.listSumIncreRecords.size());
//        Mysql mysql = new Mysql();
//        Connection conn = mysql.createConnect();

        DirectorySnapshotRealtime.ODListcreRecords.clear();
        String fileNameTimePeriod = new String();
        if (Conf.mode == -1) {


            //如果需要保留每个周期最后一条记录，则要注释掉这句
            DirectorySnapshotRealtime.listSumIncreRecords.clear();
//            DirectorySnapshotRealtime.listSumIncreRecords.putAll(DirectorySnapshotRealtime.tmpListSumIncreRecords);
//            DirectorySnapshotRealtime.tmpListSumIncreRecords.clear();

            //从mysql中取出数据
//            String BT = GpipeInit.writeLogs("--info--:OD逻辑开始处理 data 从" + DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime + "时段的数据");
//
//            ResultSet resultSet = mysql.readMysql("select * from jw2_list_nbr_group_bts_sum where date='" + date + "' and timePeriod ='" + DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime + "'", conn);
//            try {
//                while (resultSet.next()) {
//                    SignalList sl = new SignalList(resultSet.getString(ConfOfMysqlModel.listSumModelParas[2]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[3]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[4]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[5]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[6]));
//                    List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]));
//                    if (val == null) {
//                        val = new ArrayList<SignalList>();
//                    }
//                    val.add(sl);
//                    DirectorySnapshotRealtime.listSumIncreRecords.put(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), val);
//                }
//                resultSet.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }

            //--info--从本地取出本周期数据listSum
            //文件格式：date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime
            GpipeInit.writeLogs("--info--从本地取出本周期数据listSum");
            fileNameTimePeriod = DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime;
            Tools tools = new Tools();
            String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath + File.separator + date + "-" + fileNameTimePeriod + ".txt";
//            BufferedReader readerLoc = tools.readLocFile(filePath);
//            String lineLoc = null;
//            try {
//                if (readerLoc != null) {
//                    while ((lineLoc = readerLoc.readLine()) != null) {
//                        String[] cols = lineLoc.split("\\|", -1);
//                        SignalList sl = new SignalList(cols[5], cols[6], cols[3], cols[7], cols[8], cols[2]);
//                        List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(cols[3]);
//                        if (val == null) {
//                            val = new ArrayList<SignalList>();
//                        }
//                        val.add(sl);
//                        DirectorySnapshotRealtime.listSumIncreRecords.put(cols[3], val);
//                    }
//                    readerLoc.close();
//                }
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            tools.getListSum(filePath);

        } else {
            String beginTimePeriod = GpipeInit.get15minBefore(time);
            String endTimePeriod = GpipeInit.getIncreTimePeriod(beginTimePeriod, InputProperties.inputdataConf.get("timePeriod"), "HHmm");
            fileNameTimePeriod = beginTimePeriod + "-" + endTimePeriod;
            Tools tools = new Tools();
            String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath + File.separator + date + "-" + fileNameTimePeriod + ".txt";
            tools.getListSum(filePath);


        }


        //先临时数据存放在mysql,读出上一个周期最后一条记录放入DirectorySnapshotRealtime.listSumIncreRecords
//        ResultSet resultSetTmp = mysql.readMysql("select * from jw2_list_nbr_group_bts_sum_ODTmp", conn);
//        try {
//            while (resultSetTmp.next()) {
//                SignalList sl = new SignalList(resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[2]), resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[3]), resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[1]), resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[4]), resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[5]), resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[6]));
//                List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[1]));
//                if (val == null) {
//                    val = new ArrayList<SignalList>();
//                }
//                val.add(sl);
//                DirectorySnapshotRealtime.listSumIncreRecords.put(resultSetTmp.getString(ConfOfMysqlModel.listSumModelParas[1]), val);
//            }
//            resultSetTmp.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        //先临时数据存放在mysql,读出上一个周期最后一条记录放入DirectorySnapshotRealtime.listSumIncreRecords
        //临时数据存放在本地
        //文件格式：date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime
        GpipeInit.writeLogs("--info--从本地取出上一个周期每个号码最后一条记录数据listSum");
        Tools tools = new Tools();
        String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + "ODList.txt";
        BufferedReader readerLoc = tools.readLocFile(filePath);
        String lineLoc = null;
        try {
            if (readerLoc != null) {
                while ((lineLoc = readerLoc.readLine()) != null) {
//                    System.out.println(lineLoc);
                    String[] cols = lineLoc.split("\\|", -1);
                    SignalList sl = new SignalList(cols[5], cols[6], cols[3], cols[7], cols[8], cols[2]);
                    List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(cols[3]);
                    if (val == null) {
                        val = new ArrayList<SignalList>();
                    }
                    val.add(sl);
                    DirectorySnapshotRealtime.listSumIncreRecords.put(cols[3], val);
                }
                readerLoc.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        Iterator iterSignalList = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
        GpipeInit.writeLogs("--info--:ODProcess listSumIncreRecords.size（号码数）=" + DirectorySnapshotRealtime.listSumIncreRecords.size());
        while (iterSignalList.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
            String key = entry.getKey();
            List<SignalList> val = entry.getValue();
            Collections.sort(val, new Comparator<SignalList>() {
                @Override
                public int compare(SignalList o1, SignalList o2) {
                    return o1.getStartTime().compareTo(o2.getStartTime());
                }
            });

            //this is sum(the same lac|ci 's duration)
            List<SignalList> result = new ArrayList<SignalList>();
            SignalList beforeSL = new SignalList();
            for (SignalList signalList : val) {
                if (beforeSL.getLacCi().equals(signalList.getLacCi())) {
                    beforeSL.setEndTime(signalList.getEndTime());
                } else {
                    if (GpipeInit.isNotEmpty(beforeSL.getNbr())) {
                        result.add(beforeSL);
                    }
                    beforeSL = signalList.clone();
                }
            }
            result.add(beforeSL);

            DirectorySnapshotRealtime.listSumIncreRecords.put(key, result);
        }
//        }
        String initET = GpipeInit.writeLogs("--------DirectorySnapshotRealtime.listSumIncreRecords initial success!-second period--------DirectorySnapshotRealtime.listSumIncreRecords.size（号码数）=" + DirectorySnapshotRealtime.listSumIncreRecords.size());
        GpipeInit.writeLogs("--info--:listSumIncreRecords initial total time :" + GpipeInit.duration(ST, initET, "yyyy-MM-dd HH:mm:ss") + "s");


        String ODST = GpipeInit.writeLogs("--------OD data dual process !-second period--------");
        List<ODList> listOD = new ArrayList<ODList>();
        List<String[]> in = new ArrayList<>();
        Iterator iterOD = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
        int i = 0;
        int count = 0;
        StringBuilder listSumTmp = new StringBuilder();
        StringBuilder ODListStr = new StringBuilder();
        String filePathODList = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.ODLPath + File.separator + date + "-" + fileNameTimePeriod + ".txt";

        while (iterOD.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterOD.next();
            String key = entry.getKey();
            List<SignalList> val = entry.getValue();
//            if (val.size() <= 1) continue;
            SignalList beforeSL = new SignalList();
            for (SignalList sl : val) {
                i++;
//                转换为od
                if (beforeSL.getNbr() == null) {
                    beforeSL = sl.clone();
                    continue;
                }
                ODList od = new ODList(date, DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime, key, beforeSL.getStartTime(), sl.getEndTime(), beforeSL.getLacCi(), sl.getLacCi());
                listOD.add(od);
                beforeSL = sl.clone();
                String[] s = new String[]{date, DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime, key, od.getBeginTime(), od.getArriveTime(), String.valueOf(od.getDuration()), od.getLacCiO(), od.getLacCiD(), String.valueOf(od.getDistance())};
                in.add(s);
                //数据格式date,timePeriod,nbr,beginTime,arriveTime,duration,lacCiO,lacCiD,distance
                ODListStr.append(date + "|" + DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime + "|" + key + "|" + od.getBeginTime() + "|" + od.getArriveTime() + "|" + String.valueOf(od.getDuration()) + "|" + od.getLacCiO() + "|" + od.getLacCiD() + "|" + String.valueOf(od.getDistance()) + "\n");
            }

            count++;
//            String[] str = new String[]{beforeSL.getDate(), beforeSL.getHour(), beforeSL.getTimePeriod(), beforeSL.getNbr(), "-1", beforeSL.getLac(), beforeSL.getCi(), beforeSL.getStartTime(), beforeSL.getEndTime(), "ODProcess"};

//            listSumTmp.add(str);
            listSumTmp.append(beforeSL.getDate() + "|" + beforeSL.getHour() + "|" + beforeSL.getTimePeriod() + "|" + beforeSL.getNbr() + "|" + "-1" + "|" + beforeSL.getLac() + "|" + beforeSL.getCi() + "|" + beforeSL.getStartTime() + "|" + beforeSL.getEndTime() + "\n");


//            DirectorySnapshotRealtime.ODListcreRecords.put(key, listOD);


        }

//        GpipeInit.writeLogs("--info--:listSumTmp="+listSumTmp.size());

        //数据存放在mysql
//        String tmpSql = "INSERT INTO jw2_list_nbr_group_bts_sum_ODTmp(date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime,stage)VALUES(?,?,?,?,?,?,?,?,?,?)";
//        String delSql = "truncate table  jw2_list_nbr_group_bts_sum_ODTmp";
//        mysql.deleteMysql(delSql, conn);
//        mysql.writeMysql(listSumTmp, tmpSql, conn);
//        listSumTmp.clear();


        String TmpST = GpipeInit.writeLogs("--------listSum data write local begin !-second period--------odlist.size（记录数）=" + in.size());

        //本周期每个号码最后一条记录数据存放在本地
        GpipeInit.writeLogs("--info--listSumTmp(每个号码最后一条记录)=" + count);
        tools.writeLocFile(filePath, listSumTmp.toString());
        listSumTmp.delete(0, listSumTmp.length());

        String TmpET = GpipeInit.writeLogs("--------listSum data write local success !-second period--------odlist.size=" + in.size());
        GpipeInit.writeLogs("--info--:listSum data write local total time :" + GpipeInit.duration(TmpST, TmpET, "yyyy-MM-dd HH:mm:ss") + "s");


        String ODET = GpipeInit.writeLogs("--------OD data dual success !-second period--------odlist.size=" + in.size());
        GpipeInit.writeLogs("--info--:OD data dual process total time :" + GpipeInit.duration(ODST, ODET, "yyyy-MM-dd HH:mm:ss") + "s");

        String writeODST = GpipeInit.writeLogs("--------begin to write OD data into local !-second period--------");


        //jw2_app_od_list数据存放在mysql
//        String sql = "INSERT INTO jw2_app_od_list(date,timePeriod,nbr,beginTime,arriveTime,duration,lacCiO,lacCiD,distance) VALUES (?,?,?,?,?,?,?,?,?)";
//        mysql.writeMysql(in, sql, conn);
//        in.clear();

        //jw2_app_od_list数据存放在本地
        //数据格式date,timePeriod,nbr,beginTime,arriveTime,duration,lacCiO,lacCiD,distance
        GpipeInit.writeLogs("--info--ODListStr(ODList 记录数)=" + i);
        tools.writeLocFile(filePathODList, ODListStr.toString());
        ODListStr.delete(0, ODListStr.length());


//        try {
//            conn.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        String writeODET = GpipeInit.writeLogs("--------finish writeing OD data into mysql !-second period--------");
        GpipeInit.writeLogs("--info--:write OD data into mysql total time :" + GpipeInit.duration(writeODST, writeODET, "yyyy-MM-dd HH:mm:ss") + "s");


        String ET = GpipeInit.writeLogs("--------OD process success!-second period--------");
        GpipeInit.writeLogs("--info--:OD process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss") + "s");
    }

    /**
     * 实时接口：需要改造实时接口写入jw2_list_nbr_group_bts_sum的逻辑，体现出timePeriod字段出来。
     * 批量接口：已经测试完成
     */

    public void workLifeDualOptimize() {


//        String time = "201707312355111".substring(8, 12);
//        String date = "201707312355111".substring(0, 8);
//        String month = "201707312355111".substring(0, 6);

        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);
        String month = fileTime.substring(0, 6);

//        String monthbefore = GpipeInit.changeMonth(month, ConfOfMysqlModel.monOffset);


        //数据到了晚上11点到12点时候开始处理一天的数据；2330~2359没有数据但是begintime已经到了2345~0000这个时间段
        //注意DirectorySnapshotRealtime.periodBeginTime的初始值在正常跑数的时候会导致判断错误
        if ((Long.parseLong(time) > 2345 && Long.parseLong(time) <= 2359) || dayEndFlag.equals("0000")) {
            //每天职住数据处理
            String ST = GpipeInit.writeLogs("--------work life day process!-second period--------");

            //按照每15分钟处理居住数据
            String lifeST = GpipeInit.writeLogs("--info--:--------life process!-second period--------");
            workLifePeriodDual("life", date);
            String lifeET = GpipeInit.writeLogs("--info--:--------life process success!-second period--------");
            GpipeInit.writeLogs("--info--:life process total time :" + GpipeInit.duration(lifeST, lifeET, "yyyy-MM-dd HH:mm:ss") + "s");

            //按照每15分钟处理工作数据
            String workST = GpipeInit.writeLogs("--info--:--------work process!-second period--------");
            workLifePeriodDual("work", date);
            String workET = GpipeInit.writeLogs("--info--:--------work process success!-second period--------");
            GpipeInit.writeLogs("--info--:work process total time :" + GpipeInit.duration(workST, workET, "yyyy-MM-dd HH:mm:ss") + "s");
            String ET = GpipeInit.writeLogs("--------work life day success!-second period--------");
            GpipeInit.writeLogs("--info--:work life day  process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss") + "s");

        }
    }

    //, Map<String, Map<String, SignalList>> workLifeResultMap
    private void workLifePeriodDual(String flag, String date) {

        String startTime = new String();
        String finalTime = new String();

        if (flag.equals("life")) {
            startTime = "0000";
            finalTime = "0700";
        } else if (flag.equals("work")) {
            startTime = "0700";
            finalTime = "1800";
        }

        Mysql mysql = new Mysql();
        Connection conn = mysql.createConnect();

        while (!startTime.equals(finalTime)) {
            //从jw2_list_nbr_group_bts_sum读出每15分钟的记录出来
            String endTime = GpipeInit.getIncreTimePeriodOptional(startTime, InputProperties.inputdataConf.get("timePeriod"), "HHmm", "HHmm");
//            GpipeInit.getIncreTimePeriod(startTime, InputProperties.inputdataConf.get("timePeriod"), "HHmm");
            String BT = GpipeInit.writeLogs("--info--:开始处理 data 从" + startTime + "-" + endTime + "时段的数据");

            //从mysql中读出清单数据
//            ResultSet resultSet = mysql.readMysql("select * from jw2_list_nbr_group_bts_sum where date='" + date + "' and timePeriod ='" + startTime + "-" + endTime + "'", conn);
//
//            int flag1 = -1;
//            try {
//                while (resultSet.next()) {
//                    flag1 = 1;
//                    SignalList sl = new SignalList(resultSet.getString(ConfOfMysqlModel.listSumModelParas[2]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[3]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[4]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[5]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[6]));
//                    List<SignalList> val = DirectorySnapshotRealtime.listSumPeriodRecords.get(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]));
//                    if (val == null) {
//                        val = new ArrayList<SignalList>();
//                    }
//                    val.add(sl);
//                    DirectorySnapshotRealtime.listSumPeriodRecords.put(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), val);
//                }
//                resultSet.close();
//
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }


            //从本地读出数据
            //文件格式：date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime

            Tools tools = new Tools();
            String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath + File.separator + date + "-" + startTime + "-" + endTime + ".txt";
            BufferedReader readerLoc = tools.readLocFile(filePath);
            String lineLoc = null;
            int flag1 = -1;
            try {
                if (readerLoc != null) {
                    while ((lineLoc = readerLoc.readLine()) != null) {
                        flag1 = 1;
                        String[] cols = lineLoc.split("\\|", -1);
                        SignalList sl = new SignalList(cols[5], cols[6], cols[3], cols[7], cols[8], cols[2]);
                        List<SignalList> val = DirectorySnapshotRealtime.listSumPeriodRecords.get(cols[3]);
                        if (val == null) {
                            val = new ArrayList<SignalList>();
                        }
                        val.add(sl);
                        DirectorySnapshotRealtime.listSumPeriodRecords.put(cols[3], val);
                    }
                    readerLoc.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

            GpipeInit.writeLogs("--test2--:" + DirectorySnapshotRealtime.listSumPeriodRecords.size());

            if (DirectorySnapshotRealtime.listSumPeriodRecords.size() > 0 && flag1 == 1) {

                //对这个周期（每15分钟）的数据进行排序，然后按照相同的lac|ci聚合
                Iterator iterSignalList = DirectorySnapshotRealtime.listSumPeriodRecords.entrySet().iterator();
                int i = 0;
                while (iterSignalList.hasNext()) {
                    Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
                    String key = entry.getKey();
                    List<SignalList> val = entry.getValue();
////                try {
//                    Collections.sort(val, new Comparator<SignalList>() {
//                        @Override
//                        public int compare(SignalList o1, SignalList o2) {
//                            return o1.getStartTime().compareTo(o2.getStartTime());
//                        }
//                    });
//                }catch (NullPointerException e){
//                    System.out.println("error");
//                }

                    //this is sum(the same lac|ci 's duration)
                    List<SignalList> result = new ArrayList<SignalList>();
//                    SignalList beforeSL = new SignalList();
//                    for (SignalList signalList : val) {
//                        if (beforeSL.getLacCi().equals(signalList.getLacCi())) {
//                            beforeSL.setEndTime(signalList.getEndTime());
//                        } else {
//                            if (GpipeInit.isNotEmpty(beforeSL.getNbr())) {
//                                result.add(beforeSL);
//                            }
//                            beforeSL = signalList.clone();
//                        }
//                    }
//                    result.add(beforeSL);

                    //找出DirectorySnapshotRealtime.listSumPeriodRecords中最长和最新的记录
//                    SignalList slLongTime = new SignalList();
//                    SignalList slLast = new SignalList();
                    SignalList slLongest = new SignalList();
                    for (SignalList sl : val) {//change

                        if (sl.getDuration() > slLongest.getDuration()) {
                            slLongest = sl.clone();
                        }

//                        if (sl.getDuration() > slLongTime.getDuration()) {
//                            slLongTime = sl.clone();
//                        }
//                        if (GpipeInit.isEmpty(slLast.getStartTime())) {
//                            slLast = sl.clone();
//                        } else if (GpipeInit.str1bigThenstr2(sl.getStartTime(), slLast.getStartTime())) {
//                            slLast = sl.clone();
//                        }
//                        if (slLast.getDuration() > slLongTime.getDuration()) {
//                            slLongest = slLast.clone();
//                        } else {
//                            slLongest = slLongTime.clone();
//                        }

                    }

                    result.clear();
//                    result.add(slLongTime);
//                    result.add(slLast);
                    result.add(slLongest);

                    i++;
                    DirectorySnapshotRealtime.listSumPeriodRecords.put(key, result);
                }

                GpipeInit.writeLogs("--test4--:" + DirectorySnapshotRealtime.listSumPeriodRecords.size());

            }

//            GpipeInit.writeLogs("--test--:in-in--workLifeResultMap:" + workLifeResultMap.size());


            String ET = GpipeInit.writeLogs("--info--:完成处理 data 从" + startTime + "-" + endTime + "时段的数据");
            GpipeInit.writeLogs("--info--:data 从" + startTime + "-" + endTime + "时段的数据,total time :" + GpipeInit.duration(BT, ET, "yyyy-MM-dd HH:mm:ss") + "s");
            startTime = endTime;
        }
        //从表jw2_app_work_life_day中读出当天的记录
        String sql = "select * from jw2_app_work_life_day where date='" + date + "'";
        ResultSet resultSet = mysql.readMysql(sql, conn);
        Map<String, WorkLife> tmpRecords = new HashMap<String, WorkLife>();
        try {
            while (resultSet.next()) {
                WorkLife wl = new WorkLife();
                wl.setWorkBts(resultSet.getString(ConfOfMysqlModel.workLifeModelParas[2]));
                wl.setLifeBts(resultSet.getString(ConfOfMysqlModel.workLifeModelParas[3]));
                String nbr = resultSet.getString(ConfOfMysqlModel.workLifeModelParas[1]);
                tmpRecords.put(nbr, wl);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //此处选出该flag（work,life）对应的时长最长基站信息
        Iterator iterSignalList = DirectorySnapshotRealtime.listSumPeriodRecords.entrySet().iterator();
        while (iterSignalList.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
            String key = entry.getKey();
            List<SignalList> val = entry.getValue();
            SignalList slLongest = new SignalList();
            for (SignalList sl : val) {
                if (sl.getDuration() > slLongest.getDuration()) {
                    slLongest = sl.clone();
                }
            }
            WorkLife wl = tmpRecords.get(key);
            if (wl == null) {
                wl = new WorkLife();
            }
            if (flag.equals("work")) {
                wl.setWorkBts(slLongest.getLacCi());
            } else if (flag.equals("life")) {
                wl.setLifeBts(slLongest.getLacCi());
            }
            tmpRecords.put(key, wl);
        }

        DirectorySnapshotRealtime.listSumPeriodRecords.clear();
        String date7Day = GpipeInit.getIncreDayPeriodOptional(date, Conf.delPeriod, "yyyyMMdd", "yyyyMMdd");
        String wSql = "INSERT INTO jw2_app_work_life_day(date,nbr,workBts,lifeBts)VALUES(?,?,?,?)";
        String dSql = "delete from jw2_app_work_life_day where date='" + date + "' and (workBts is null or lifeBts is null)";
        String dSqlThis = "delete from jw2_app_work_life_day where date='" + date + "'";
        String dSqlBefore = "delete from jw2_app_work_life_day where date ='" + date7Day + "'";

        mysql.updateMysql(dSqlBefore, conn);
        mysql.updateMysql(dSqlThis, conn);

        List<String[]> in = new ArrayList<>();

        Iterator workLifeList = tmpRecords.entrySet().iterator();
        while (workLifeList.hasNext()) {
            Map.Entry<String, WorkLife> entry = (Map.Entry) workLifeList.next();
            String nbr = entry.getKey();
            WorkLife wl = entry.getValue();
            String[] s = new String[]{date, nbr, wl.getWorkBts(), wl.getLifeBts()};
            in.add(s);
        }
        GpipeInit.writeLogs("--test--:in--workLifeResultMap:" + tmpRecords.size());
        tmpRecords.clear();
        mysql.writeMysql(in, wSql, conn);
        mysql.deleteMysql(dSql, conn);
        in.clear();
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void workLifeDual() {
        //每天职住数据处理
        String ST = GpipeInit.writeLogs("--------work life process!-second period--------");
//        String time = "201707312355111".substring(8, 12);
//        String date = "201707312355111".substring(0, 8);
//        String month = "201707312355111".substring(0, 6);

        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);
        String month = fileTime.substring(0, 6);

        String monthbefore = GpipeInit.changeMonth(month, ConfOfMysqlModel.monOffset);

        List<String[]> in = new ArrayList<>();


        //数据到了晚上11点到12点时候开始处理一天的数据；2330~2359没有数据但是begintime已经到了2345~0000这个时间段
        if ((Long.parseLong(time) > 2330 && Long.parseLong(time) <= 2359) || DirectorySnapshotRealtime.periodBeginTime.equals("0000")) {
//        if (true) {
            //开始处理一天的数据

            //nbr:Object(lac|ci|nbr|starttime|endtime|duration) -- second period
//            Map<String,WorkLife> workLifeRecords = new HashMap<String, WorkLife>();//备用

//            此处应改成按照工作、居住的时间段，按15分钟提取数据进行分析
            //找出在职住时间段内时长最长的记录
            Iterator iterSignalList = DirectorySnapshotRealtime.listSumRecords.entrySet().iterator();
            while (iterSignalList.hasNext()) {
                Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
                String key = entry.getKey();
                List<SignalList> val = entry.getValue();

                SignalList workSL = new SignalList();
                SignalList lifeSL = new SignalList();
//                WorkLife wl = new WorkLife();

                for (SignalList signalList : val) {
                    if (signalList.isWork()) {
                        if (signalList.getDuration() - workSL.getDuration() >= 0) {
                            workSL = signalList.clone();
                        }
                    } else if (signalList.isLife()) {
                        if (signalList.getDuration() - lifeSL.getDuration() >= 0) {
                            lifeSL = signalList.clone();
                        }
                    }
                }

//                以上部分需要修改，下面部分可保持不变
                String[] s = new String[]{date, key, workSL.getLacCi(), lifeSL.getLacCi()};
                in.add(s);

                //备用，以防在其他逻辑中需要用到这部分数据
//                wl.setLifeBts(lifeSL.getLacCi());
//                wl.setWorkBts(workSL.getLacCi());
//                workLifeRecords.put(key,wl);
            }
            //把workLifeRecords入库到mysql中
            Mysql mysql = new Mysql();
            Connection conn = mysql.createConnect();
            String sql = "INSERT INTO jw2_app_work_life_day(date,nbr,workBts,lifeBts)VALUES(?,?,?,?)";
            mysql.writeMysql(in, sql, conn);
            in.clear();

            //清空这一天的DirectorySnapshotRealtime.listSumRecords
            DirectorySnapshotRealtime.listSumRecords.clear();

            //开始处理一个月的数据
            sql = "select * from jw2_app_work_life_day where date like '" + month + "%'";
            if (GpipeInit.isLastDayOfMonth(date)) {
                ResultSet resultSet = mysql.readMysql(sql, conn);

                //nbr|lac|ci:num
                HashMap<String, AtomicInteger> monthMap1 = new HashMap<String, AtomicInteger>();
                HashMap<String, AtomicInteger> monthMap2 = new HashMap<String, AtomicInteger>();

                try {
                    AtomicInteger counter = new AtomicInteger(0);
                    while (resultSet.next()) {
                        String keyWork = resultSet.getString(ConfOfMysqlModel.workLifeModelParas[1]) + "|" + resultSet.getString(ConfOfMysqlModel.workLifeModelParas[2]);
                        String keyLife = resultSet.getString(ConfOfMysqlModel.workLifeModelParas[1]) + "|" + resultSet.getString(ConfOfMysqlModel.workLifeModelParas[3]);

                        //工作地的处理
                        if (GpipeInit.isNotEmpty(String.valueOf(monthMap1.get(keyWork)))) {
                            monthMap1.get(keyWork).incrementAndGet();
                        } else {
                            monthMap1.put(keyWork, new AtomicInteger());
                        }

                        //居住地的处理
                        if (GpipeInit.isNotEmpty(String.valueOf(monthMap2.get(keyLife)))) {
                            monthMap2.get(keyLife).incrementAndGet();
                        } else {
                            monthMap2.put(keyLife, new AtomicInteger());
                        }
                    }
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                //工作地：找出出现天数最多的基站
                HashMap<String, String> monthMap3 = new HashMap<String, String>();
                Iterator iterWork = monthMap1.entrySet().iterator();

                while (iterWork.hasNext()) {
                    Map.Entry<String, AtomicInteger> entry = (Map.Entry) iterWork.next();
                    String key = entry.getKey();
                    AtomicInteger val = entry.getValue();
                    String[] keys = key.split("\\|");
                    if (keys.length == 3) {
                        String nbr = keys[0];
                        String lacCi = keys[1] + "|" + keys[2];
                        if (GpipeInit.isNotEmpty(monthMap3.get(nbr))) {
                            if (GpipeInit.str1bigstr2(val.toString(), monthMap3.get(nbr).split("\\|")[2])) {
                                monthMap3.put(nbr, lacCi + "|" + val.toString());
                            }
                        } else {
                            monthMap3.put(nbr, lacCi + "|" + val.toString());
                        }
                    }
                }
                monthMap1.clear();

                //居住地：找出出现天数最多的基站
                //monthMap4--nbr:lacci|daysum
                HashMap<String, String> monthMap4 = new HashMap<String, String>();
                Iterator iterLife = monthMap2.entrySet().iterator();

                while (iterLife.hasNext()) {
                    Map.Entry<String, AtomicInteger> entry = (Map.Entry) iterLife.next();
                    String key = entry.getKey();
                    AtomicInteger val = entry.getValue();
                    String[] keys = key.split("\\|");
                    if (keys.length == 3) {
                        String nbr = keys[0];
                        String lacCi = keys[1] + "|" + keys[2];
                        if (GpipeInit.isNotEmpty(monthMap4.get(nbr))) {
                            if (GpipeInit.str1bigstr2(val.toString(), monthMap4.get(nbr).split("\\|")[2])) {
                                monthMap4.put(nbr, lacCi + "|" + val.toString());
                            }
                        } else {
                            monthMap4.put(nbr, lacCi + "|" + val.toString());
                        }
                    }
                }
                monthMap2.clear();
                //合并居住地工作地
                //monthMap5--nbr:worklacci,area|lifelacci,area
                HashMap<String, WorkLife> monthMap5 = new HashMap<String, WorkLife>();
                Iterator iterCombine = monthMap3.entrySet().iterator();
                while (iterCombine.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) iterCombine.next();
                    String key = entry.getKey();
                    String val = entry.getValue();
                    WorkLife wl = new WorkLife();
                    wl.setWorkBts((val.split("\\|")[0] + "|" + val.split("\\|")[1]));
                    monthMap5.put(key, wl);
                }
                monthMap3.clear();

                Iterator iterCombine1 = monthMap4.entrySet().iterator();
                while (iterCombine1.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) iterCombine1.next();
                    String key = entry.getKey();
                    String val = entry.getValue();
                    if (monthMap5.get(key) != null) {
                        WorkLife wl = monthMap5.get(key);
                        wl.setLifeBts((val.split("\\|")[0] + "|" + val.split("\\|")[1]));
                        monthMap5.put(key, wl);
                    } else {
                        WorkLife wl = new WorkLife();
                        wl.setLifeBts((val.split("\\|")[0] + "|" + val.split("\\|")[1]));
                        monthMap5.put(key, wl);
                    }
                }
                monthMap4.clear();

                HashMap<String, WorkLifeSum> monthMapInMysql = new HashMap<String, WorkLifeSum>();
                Iterator iterCombine3 = monthMap5.entrySet().iterator();
                while (iterCombine3.hasNext()) {
                    Map.Entry<String, WorkLife> entry = (Map.Entry) iterCombine3.next();
//                    String nbr = entry.getKey();
                    WorkLife val = entry.getValue();
                    String newKeyWork = val.getbAreaIdWork() + "|" + val.getbAreaNameWork() + "|" + val.getmAreaIdWork() + "|" + val.getmAreaNameWork() + "|" + val.getsAreaIdWork() + "|" + val.getsAreaNameWork();
                    String newKeyLife = val.getbAreaIdLife() + "|" + val.getbAreaNameLife() + "|" + val.getmAreaIdLife() + "|" + val.getmAreaNameLife() + "|" + val.getsAreaIdLife() + "|" + val.getsAreaNameLife();
                    if (monthMapInMysql.get(newKeyWork) == null) {
                        WorkLifeSum wls = new WorkLifeSum();
                        wls.getWorkSum().incrementAndGet();
                        monthMapInMysql.put(newKeyWork, wls);
                    } else {
                        monthMapInMysql.get(newKeyWork).getWorkSum().incrementAndGet();
                    }
                    if (monthMapInMysql.get(newKeyLife) == null) {
                        WorkLifeSum wls = new WorkLifeSum();
                        wls.getLifeSum().incrementAndGet();
                        monthMapInMysql.put(newKeyLife, wls);
                    } else {
                        monthMapInMysql.get(newKeyLife).getLifeSum().incrementAndGet();
                    }
                }


                //monthMapInMysql------b_area_id|b_area_name|m_area_id|m_area_name|s_area_id|s_area_name:<workSum:num;lifeSum:num>
                Iterator iterCombine4 = monthMapInMysql.entrySet().iterator();
                List<String[]> inMonSum = new ArrayList<>();
                while (iterCombine4.hasNext()) {
                    Map.Entry<String, WorkLifeSum> entry = (Map.Entry) iterCombine4.next();
                    String[] area = entry.getKey().split("\\|");
                    WorkLifeSum val = entry.getValue();
                    String[] s = new String[]{month, area[0], area[0], area[0], area[0], area[0], area[0], val.getWorkSum().toString(), val.getLifeSum().toString()};
                    inMonSum.add(s);
                }
                monthMapInMysql.clear();

                //jw2_app_work_life_month_sum—职住信息统计月表（按月不断插入数据，同时清理一年前的数据）
                mysql.deleteMysql("delete table jw2_app_work_life_month_sum where date <='" + monthbefore + "'", conn);
                mysql.writeMysql(inMonSum, "insert into jw2_app_work_life_month_sum (date,b_area_id,b_area_name,m_area_id,m_area_name,s_area_id,s_area_name,work_num,live_num) values (?,?,?,?,?,?,?,?,?)", conn);
                inMonSum.clear();

                //monthMapInMysql------b_area_id|b_area_name|m_area_id|m_area_name|s_area_id|s_area_name:<workSum:num;lifeSum:num>
                Iterator iterCombine5 = monthMap5.entrySet().iterator();
                List<String[]> inMon = new ArrayList<>();
                while (iterCombine5.hasNext()) {
                    Map.Entry<String, WorkLife> entry = (Map.Entry) iterCombine5.next();
                    String nbr = entry.getKey();
                    WorkLife val = entry.getValue();

                    String[] s = new String[]{month, nbr, val.getWorkBts(), val.getLifeBts()};
                    inMon.add(s);
                }
                monthMap5.clear();

                //jw2_app_work_life_month_sum—职住信息统计月表（按月不断插入数据，同时清理一年前的数据）
                mysql.deleteMysql("delete table jw2_app_work_life_month where date <='" + monthbefore + "'", conn);
                mysql.writeMysql(inMon, "insert into jw2_app_work_life_month (date,nbr,workBts,lifeBts) values (?,?,?,?)", conn);
                inMon.clear();

                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }


            }
        }
        String ET = GpipeInit.writeLogs("--------work life success!-second period--------");
        GpipeInit.writeLogs("--info--:work life process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 说明：二期清单汇总层数据处理
     * 优化：把新增的记录，先排序，然后再加到之前整理过的记录后面。
     */
    public void listSumLevelOptimize() {
        String fileTime = DirectorySnapshot.lastFileName.substring(DirectorySnapshot.lastFileName.indexOf("interface_") + "interface_".length(), DirectorySnapshot.lastFileName.indexOf(".cdr"));
        String time = fileTime.substring(8, 12);
        String date = fileTime.substring(0, 8);
        String month = fileTime.substring(0, 6);

        String fileNameTimePeriod = null;
        if (Conf.mode == -1) {
            fileNameTimePeriod = DirectorySnapshotRealtime.periodBeginTime + "-" + DirectorySnapshotRealtime.periodEndTime;
        } else {
            String beginTimePeriod = GpipeInit.get15minBefore(time);
            String endTimePeriod = GpipeInit.getIncreTimePeriod(beginTimePeriod, InputProperties.inputdataConf.get("timePeriod"), "HHmm");
            fileNameTimePeriod = beginTimePeriod + "-" + endTimePeriod;
        }


        DirectorySnapshotRealtime.listSumIncreRecords.clear();
//        DirectorySnapshotRealtime.listSumIncreRecords.putAll(DirectorySnapshotRealtime.tmpListSumIncreRecords);
        String ST = GpipeInit.writeLogs("--------list sum level process!-second period--------");
        GpipeInit.writeLogs("--info--开始读取上一个周期的listsum记录");

        //先读出上一个周期最后一条记录放入DirectorySnapshotRealtime.listSumIncreRecords
//        Mysql mysql = new Mysql();
//        Connection conn = mysql.createConnect();


        //临时数据存放在mysql
//        ResultSet resultSet = mysql.readMysql("select * from jw2_list_nbr_group_bts_sum_listTmp", conn);
////            select * from jw2_list_nbr_group_bts_sum where timePeriod = '0000-0015' limit 10;
//        try {
//            while (resultSet.next()) {
//                SignalList sl = new SignalList(resultSet.getString(ConfOfMysqlModel.listSumModelParas[2]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[3]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[4]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[5]), resultSet.getString(ConfOfMysqlModel.listSumModelParas[6]));
//                List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]));
//                if (val == null) {
//                    val = new ArrayList<SignalList>();
//                }
//                val.add(sl);
//                DirectorySnapshotRealtime.listSumIncreRecords.put(resultSet.getString(ConfOfMysqlModel.listSumModelParas[1]), val);
//            }
//            resultSet.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }


        //从本地读取临时数据在本地
        //文件格式：date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime
        Tools tools = new Tools();
        String filePath = tools.getPath() + File.separator + Conf.midPath + File.separator + "listSum.txt";
        BufferedReader readerLoc = tools.readLocFile(filePath);
        String lineLoc = null;
        try {
            if (readerLoc != null) {
                while ((lineLoc = readerLoc.readLine()) != null) {
//                    System.out.println(lineLoc);
                    String[] cols = lineLoc.split("\\|", -1);
                    if (cols.length != 9) {
                        GpipeInit.writeLogs("error line=" + lineLoc);
                        continue;
                    }
                    SignalList sl = new SignalList(cols[5], cols[6], cols[3], cols[7], cols[8], cols[2]);
                    List<SignalList> val = DirectorySnapshotRealtime.listSumIncreRecords.get(cols[3]);
                    if (val == null) {
                        val = new ArrayList<SignalList>();
                    }
                    val.add(sl);
                    DirectorySnapshotRealtime.listSumIncreRecords.put(cols[3], val);
                }
                readerLoc.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        //DirectorySnapshot.brokenPointComFiles存储着新增记录的文件
        Iterator it = DirectorySnapshot.brokenPointComFiles.entrySet().iterator();
        int i = 1;
        while (it.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry) it.next();
            String _fileName = entry.getKey();
            // we need to read files and write to mysql
//            String _fileName = it.next().toString();

            // mysql.writeMysql(_fileName, connection);
            BufferedReader reader = null;
            try {
                reader = GpipeInit.unZipOutReader(_fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String line = null;
            try {
                GpipeInit.writeLogs("--test--:the " + i + " file," + _fileName + " is processing it's content");
                i++;
                while ((line = reader.readLine()) != null) {
                    // create map like (key=bsc|accnbr，value=bsc|accnbr) for
                    // reduplicating
                    String[] fields = null;
                    fields = line.split("\\|", -1);
                    if (fields.length >= maxIndex) {
                        if (GpipeInit.isNotEmpty(fields[nbrIndex])) {
//                            DirectorySnapshotRealtime
//                                    .addRecord(fields[lacIndex] + "|"
//                                            + fields[ciIndex] + "|"
//                                            + fields[nbrIndex]);

                            //second period :group by nbr order by starttime sum(the same lac|ci 's duration) in a period
                            groupingOptimize(fields[lacIndex], fields[ciIndex], fields[nbrIndex], fields[startTimeIndex], fields[endTimeIndex]);
//                            1、断点的数据 通过数据库读取
//                                    2、停止期间的数据
                        }
                    }
                }

                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


        //second period :group by nbr order by starttime sum(the same lac|ci 's duration) in a period
        //this is order by startTime
        String addET = GpipeInit.writeLogs("--DirectorySnapshotRealtime.listSumIncreRecords finish record adding , then begin to order by startTime and sum--");
        GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.listSumIncreRecords record adding total time :" + GpipeInit.duration(ST, addET, "yyyy-MM-dd HH:mm:ss") + "s");

        Iterator iterSignalList = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
        GpipeInit.writeLogs("--info--:ListSumProcess listSumIncreRecords.size=" + DirectorySnapshotRealtime.listSumIncreRecords.size());
//        List<String[]> listSumTmp = new ArrayList<>();
        StringBuilder listSumTmp = new StringBuilder();
        int count = 0;
        while (iterSignalList.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
            String key = entry.getKey();
            List<SignalList> val = entry.getValue();
            Collections.sort(val, new Comparator<SignalList>() {
                @Override
                public int compare(SignalList o1, SignalList o2) {
                    return o1.getStartTime().compareTo(o2.getStartTime());
                }
            });

            //this is sum(the same lac|ci 's duration)
            List<SignalList> result = new ArrayList<SignalList>();
            SignalList beforeSL = new SignalList();
            for (SignalList signalList : val) {
                if (beforeSL.getLacCi().equals(signalList.getLacCi())) {
                    beforeSL.setEndTime(signalList.getEndTime());
                } else {
                    if (GpipeInit.isNotEmpty(beforeSL.getNbr())) {
                        beforeSL.setEndTime(signalList.getStartTime());
                        result.add(beforeSL);
                    }
                    beforeSL = signalList.clone();
                }
            }

//            String[] str = new String[]{beforeSL.getDate(), beforeSL.getHour(), beforeSL.getTimePeriod(), beforeSL.getNbr(), "-1", beforeSL.getLac(), beforeSL.getCi(), beforeSL.getStartTime(), beforeSL.getEndTime()};
            listSumTmp.append(beforeSL.getDate() + "|" + beforeSL.getHour() + "|" + beforeSL.getTimePeriod() + "|" + beforeSL.getNbr() + "|" + "-1" + "|" + beforeSL.getLac() + "|" + beforeSL.getCi() + "|" + beforeSL.getStartTime() + "|" + beforeSL.getEndTime() + "\n");
            count++;

            //            result.add(beforeSL);
            //最好把每个号码最后一条记录保留下来
            if (result.size() != 0) {
                DirectorySnapshotRealtime.listSumIncreRecords.put(key, result);
            }

        }
        iterSignalList.remove();
        GpipeInit.writeLogs("--info--:2 ListSumProcess listSumIncreRecords.size=" + DirectorySnapshotRealtime.listSumIncreRecords.size());

        //临时数据存放在mysql
        //        String tmpSql = "INSERT INTO jw2_list_nbr_group_bts_sum_listTmp(date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime,stage)VALUES(?,?,?,?,?,?,?,?,?,?)";
//        String delSql = "truncate table jw2_list_nbr_group_bts_sum_listTmp";
//        mysql.deleteMysql(delSql, conn);
//        mysql.writeMysql(listSumTmp, tmpSql, conn);


        //本周期最后每个号码一条记录数据存放在本地
        //文件格式：date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime
        GpipeInit.writeLogs("--info--:本周期最后每个号码一条记录数据存放入本地,listSumTmp记录数=" + count);
        tools.writeLocFile(filePath, listSumTmp.toString());
        listSumTmp.delete(0, listSumTmp.length());
        GpipeInit.writeLogs("--info--listSumTmp.length(should by 0)=" + listSumTmp.length());
//        String TmpET = GpipeInit.writeLogs("--------listSum data write local success !-second period-------listSumTmp.size="+listSumTmp.length() );
//        GpipeInit.writeLogs("--info--:listSum data write local total time :" + GpipeInit.duration(TmpST, TmpET, "yyyy-MM-dd HH:mm:ss") + "s");


//        listSumTmp="";


        //DirectorySnapshotRealtime.listSumRecords备份到mysql
//        Mysql mysql = new Mysql();
//        Connection conn = mysql.createConnect();
//        mysql.deleteMysql("truncate table jw2_list_nbr_group_bts_sum", conn);
        GpipeInit.writeLogs("--test--:DirectorySnapshotRealtime.listSumIncreRecords.size=" + DirectorySnapshotRealtime.listSumIncreRecords.size());

        //数据存放在mysql
//        String sql = "INSERT INTO jw2_list_nbr_group_bts_sum(date,hour,timePeriod,nbr,latn,lac,ci,startTime,endTime)VALUES(?,?,?,?,?,?,?,?,?)";
//        List<String[]> listSum = new ArrayList<>();
//
//        Iterator iter = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
//        int count= 0;
//        int num= 0;
//
//        while (iter.hasNext()) {
//            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iter.next();
//            List<SignalList> val = entry.getValue();
//            num++;
//            for (SignalList sl : val) {
//                count++;
//                String[] str = new String[]{sl.getDate(), sl.getHour(), sl.getTimePeriod(), sl.getNbr(), "-1", sl.getLac(), sl.getCi(), sl.getStartTime(), sl.getEndTime()};
//                listSum.add(str);
//            }
//        }
//
//
//
//
//        String transformDataET = GpipeInit.writeLogs("--info--:list sum level num=" + listSum.size()+",num="+num+",count="+count);
//        GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.listSumIncreRecords record transform total time :" + GpipeInit.duration(ST, transformDataET, "yyyy-MM-dd HH:mm:ss") + "s");
//
//        mysql.writeMysql(listSum, sql, conn);
//        listSum.clear();

        //本周期的listSum数据存放在本地
        String filePathListSum = tools.getPath() + File.separator + Conf.midPath + File.separator + Conf.listSumPath + File.separator + date + "-" + fileNameTimePeriod + ".txt";

        Iterator iter = DirectorySnapshotRealtime.listSumIncreRecords.entrySet().iterator();
        count = 0;
        while (iter.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iter.next();
            List<SignalList> val = entry.getValue();
            for (SignalList sl : val) {
                count++;
//                String[] str = new String[]{sl.getDate(), sl.getHour(), sl.getTimePeriod(), sl.getNbr(), "-1", sl.getLac(), sl.getCi(), sl.getStartTime(), sl.getEndTime()};
//                listSum.add(str);
                listSumTmp.append(sl.getDate() + "|" + sl.getHour() + "|" + sl.getTimePeriod() + "|" + sl.getNbr() + "|" + "-1" + "|" + sl.getLac() + "|" + sl.getCi() + "|" + sl.getStartTime() + "|" + sl.getEndTime() + "\n");

            }
        }
        DirectorySnapshotRealtime.listSumIncreRecords.clear();
        GpipeInit.writeLogs("--info--本周期的listSum数据存放在本地，文件记录数（listSumTmp）=" + count);
        tools.writeLocFile(filePathListSum, listSumTmp.toString());
        listSumTmp.delete(0, listSumTmp.length());

        String transformDataET = GpipeInit.writeLogs("--info--:list sum level num(should be 0)=" + listSumTmp.length());
        GpipeInit.writeLogs("--info--:DirectorySnapshotRealtime.listSumIncreRecords record transform total time :" + GpipeInit.duration(ST, transformDataET, "yyyy-MM-dd HH:mm:ss") + "s");


//        try {
//            conn.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        //如果测试单个功能模块可以在这里清理，如果关联了OD/集散量的话要在后面处理
//        DirectorySnapshotRealtime.listSumIncreRecords.clear();

//        DirectorySnapshotRealtime.periodBeginTime = DirectorySnapshotRealtime.periodEndTime;
        dayEndFlag = DirectorySnapshotRealtime.periodEndTime;
        String ET = GpipeInit.writeLogs("--------list sum level success!-second period--------");
        GpipeInit.writeLogs("--info--:list sum level process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss") + "s");
    }


    /**
     * 说明：二期清单汇总层数据处理
     * 目前：现在是通过把新增的记录，和之前已经处理的记录混合一起，然后全部重新排序去重
     * 优化：把新增的记录，先排序，然后再加到之前整理过的记录后面。
     */

    public void listSumLevel() {
        String ST = GpipeInit.writeLogs("--------list sum level process!-second period--------");
//        Map<String, String> _files = DirectorySnapshot.brokenPointComFiles;
//        Collection<String> c = _files.values();
//        Iterator it = c.iterator();

        //DirectorySnapshot.brokenPointComFiles存储着新增记录的文件
        Iterator it = DirectorySnapshot.brokenPointComFiles.entrySet().iterator();
        int i = 1;
        while (it.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry) it.next();
            String _fileName = entry.getKey();
            // we need to read files and write to mysql
//            String _fileName = it.next().toString();

            // mysql.writeMysql(_fileName, connection);
            BufferedReader reader = null;
            try {
                reader = GpipeInit.unZipOutReader(_fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String line = null;
            try {
                GpipeInit.writeLogs("--test--:the " + i + " file," + _fileName + " is processing it's content");
                i++;
                while ((line = reader.readLine()) != null) {
                    // create map like (key=bsc|accnbr，value=bsc|accnbr) for
                    // reduplicating
                    String[] fields = null;
                    fields = line.split("\\|", -1);
                    if (fields.length >= maxIndex) {
                        if (GpipeInit.isNotEmpty(fields[nbrIndex])) {
//                            DirectorySnapshotRealtime
//                                    .addRecord(fields[lacIndex] + "|"
//                                            + fields[ciIndex] + "|"
//                                            + fields[nbrIndex]);

                            //second period :group by nbr order by starttime sum(the same lac|ci 's duration) in a period
                            grouping(fields[lacIndex], fields[ciIndex], fields[nbrIndex], fields[startTimeIndex], fields[endTimeIndex]);
//                            1、断点的数据 通过数据库读取
//                                    2、停止期间的数据
                        }
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
//        _files.clear();
//        c.clear();


        //second period :group by nbr order by starttime sum(the same lac|ci 's duration) in a period
        //this is order by startTime
        GpipeInit.writeLogs("--DirectorySnapshotRealtime.listSumRecords finish record adding , then begin to order by startTime and sum--");

        Iterator iterSignalList = DirectorySnapshotRealtime.listSumRecords.entrySet().iterator();
        while (iterSignalList.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iterSignalList.next();
            String key = entry.getKey();
            List<SignalList> val = entry.getValue();
            Collections.sort(val, new Comparator<SignalList>() {
                @Override
                public int compare(SignalList o1, SignalList o2) {
                    return o1.getStartTime().compareTo(o2.getStartTime());
                }
            });

            //this is sum(the same lac|ci 's duration)
            List<SignalList> result = new ArrayList<SignalList>();
            SignalList beforeSL = new SignalList();
            for (SignalList signalList : val) {
                if (beforeSL.getLacCi().equals(signalList.getLacCi())) {
                    beforeSL.setEndTime(signalList.getEndTime());
                } else {
                    if (GpipeInit.isNotEmpty(beforeSL.getNbr())) {
                        result.add(beforeSL);
                    }
                    beforeSL = signalList.clone();
                }
            }
            result.add(beforeSL);

            DirectorySnapshotRealtime.listSumRecords.put(key, result);
        }
        iterSignalList.remove();

//        //test 写出到本地文件
//        SendResultData srd = new SendResultData();
//        srd.writeLoc("E:\\testLog\\jw_test_file\\test.txt",DirectorySnapshotRealtime.listSumRecords);

        //DirectorySnapshotRealtime.listSumRecords备份到mysql
        Mysql mysql = new Mysql();
        Connection conn = mysql.createConnect();
        mysql.deleteMysql("truncate table jw2_list_nbr_group_bts_sum", conn);
        GpipeInit.writeLogs("--test--:DirectorySnapshotRealtime.listSumRecords.size=" + DirectorySnapshotRealtime.listSumRecords.size());
        String sql = "INSERT INTO jw2_list_nbr_group_bts_sum(date,nbr,latn,lac,ci,startTime,endTime)VALUES(?,?,?,?,?,?,?)";
        List<String[]> listSum = new ArrayList<>();

        Iterator iter = DirectorySnapshotRealtime.listSumRecords.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<SignalList>> entry = (Map.Entry) iter.next();
            List<SignalList> val = entry.getValue();
            for (SignalList sl : val) {
                String[] str = new String[]{sl.getDate(), sl.getNbr(), "-1", sl.getLac(), sl.getCi(), sl.getStartTime(), sl.getEndTime()};
                listSum.add(str);
            }
        }
        GpipeInit.writeLogs("--info--:list sum level num=" + listSum.size());
        mysql.writeMysql(listSum, sql, conn);
        listSum.clear();
//        mysql.writeMysql(DirectorySnapshotRealtime.listSumRecords, conn);

        String ET = GpipeInit.writeLogs("--------list sum level success!-second period--------");
        GpipeInit.writeLogs("--info--:list sum level process total time :" + GpipeInit.duration(ST, ET, "yyyy-MM-dd HH:mm:ss") + "s");
    }

    /*
     * author:zhengrx function:Processing data according to the realtime logic.
     */

    public void MysqlRealtime() {
        init();
        Tools tools = new Tools();

        Map<String, String> _files = DirectorySnapshot.files;
        Collection<String> c = _files.values();
        Iterator it = c.iterator();

        while (it.hasNext()) {
            // we need to read files and write to mysql
            String _fileName = it.next().toString();
            BufferedReader reader = null;
            try {
                reader = GpipeInit.unZipOutReader(_fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    // create map like (key=bsc|accnbr，value=bsc|accnbr) for
                    // reduplicating
                    String[] fields = null;
                    fields = line.split("\\|", -1);
                    if (fields.length >= maxIndex) {
                        if (GpipeInit.isNotEmpty(fields[nbrIndex])) {
                            DirectorySnapshotRealtime.addRecord(fields[lacIndex] + "|" + fields[ciIndex] + "|" + fields[nbrIndex]);
                        }
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        it.remove();
        c.clear();
        _files.clear();

        String[] tmpStr = null;
        // According key=base,value=nbrnum to count the people
        Iterator iter = DirectorySnapshotRealtime.records.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            tmpStr = String.valueOf(key).split("\\|", -1);
            String keyCh = tmpStr[0] + "|" + tmpStr[1];

            if (DirectorySnapshotRealtime.midResults.containsKey(keyCh)) {
                DirectorySnapshotRealtime.midResults.put(keyCh, DirectorySnapshotRealtime.midResults.get(keyCh) + 1);
            } else {
                DirectorySnapshotRealtime.midResults.put(keyCh, 1);
            }

            // produce table1.2's data.
            if (InputProperties.basestation.containsKey(keyCh)) {
                String val = (String) InputProperties.basestation.get(keyCh);
                if (!DirectorySnapshotRealtime.nbrLongLaRes.containsKey(val.split("\\|", -1)[0] + "|" + val.split("\\|", -1)[1] + "|" + tmpStr[2])) {
                    DirectorySnapshotRealtime.nbrLongLaRes.put(val.split("\\|", -1)[0] + "|" + val.split("\\|", -1)[1] + "|" + tmpStr[2], InputProperties.basestation.get(keyCh) + "|" + tmpStr[2]);
                }
            }
        }
        iter.remove();

        CommonPart();
        // begin to deal with some basic materials first period-simple----
        if (!DirectorySnapshotRealtime.records.isEmpty()) {
            GpipeInit.writeLogs("begin to deal with some basic materials first period-simple");
            ThreadRTBase threadRTBase = new ThreadRTBase("basic materials first period-simple");
            threadRTBase.start();
        }

        // begin to deal with the realtime.function 1 for first
        // period-simple----
        if (!DirectorySnapshotRealtime.midResults.isEmpty()) {
            GpipeInit.writeLogs("begin to deal with the realtime.function 1 for first period-simple");
            ThreadRTRegion threadRTRegion = new ThreadRTRegion("realtime");
            threadRTRegion.start();
        }

		/*
         * //begin to deal with the realtimenbrcome.function 2
		 * if(!DirectorySnapshotRealtime.nbrLongLaRes.isEmpty()){
		 * GpipeInit.writeLogs
		 * ("begin to deal with the realtimenbrcome.function 2");
		 * ThreadRealtimeNbrCome threadRealtimeNbrCome = new
		 * ThreadRealtimeNbrCome("realtimenbrcome.function 2");
		 * threadRealtimeNbrCome.start(); }
		 */

        // begin to deal with the realtimenbrcome.function 2 for first
        // period-simple----
        if (!DirectorySnapshotRealtime.records.isEmpty()) {
            GpipeInit.writeLogs("begin to deal with the realtimenbrcome.function 2 for first period-simple");
            ThreadRTRegionCome threadRTRegionCome = new ThreadRTRegionCome("realtimecome");
            threadRTRegionCome.start();
        }

        // begin to deal with the realtimenbrgo.function 3 for first
        // period-simple----
        if (!DirectorySnapshotRealtime.records.isEmpty()) {
            GpipeInit.writeLogs("begin to deal with the realtimenbrgo.function 3 for first period-simple");
            ThreadRTRegionGo threadRTRegionGo = new ThreadRTRegionGo("realtimego");
            threadRTRegionGo.start();
        }

        Iterator iter1 = InputProperties.basestation.entrySet().iterator();
        while (iter1.hasNext()) {
            Map.Entry entry = (Map.Entry) iter1.next();
            Object key = entry.getKey();
            String value = (String) entry.getValue();
            value = value.split("\\|", -1)[0] + "|" + value.split("\\|", -1)[1];
            if (DirectorySnapshotRealtime.midResults.containsKey(key)) {
                if (DirectorySnapshotRealtime.results.containsKey(value)) {
                    DirectorySnapshotRealtime.results.put((String) value, DirectorySnapshotRealtime.results.get(value) + DirectorySnapshotRealtime.midResults.get(key));
                } else {
                    DirectorySnapshotRealtime.results.put((String) value, DirectorySnapshotRealtime.midResults.get(key));
                }
            }
        }
        iter1.remove();
        // begin to deal with the realtimenbr.function 1 for first period----
        // Write the result data to mysql----LONGITUDE,LATITUDE,NBR_NUM
        // if(!DirectorySnapshotRealtime.results.isEmpty()){
        // String insertTruncate = new
        // String("TRUNCATE TABLE subject_result_realtime");
        // String insertQuery = new
        // String("INSERT INTO subject_result_realtime(LONGITUDE,LATITUDE,NBR_NUM)VALUES(?,?,?)");
        // SendResultData sendResultData = new SendResultData();
        // sendResultData.writeMysql(DirectorySnapshotRealtime.results,
        // insertQuery,3,insertTruncate,"realtime.function 1 for first period");
        // }


    }

    public static void main(String[] args) {
        LoadDataByDirectory l = new LoadDataByDirectory();
        l.test("20170901");

    }


}
